package org.hawk.gwt.ppc.scanner.utils;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import org.hawk.gwt.ppc.utils.Name;

/**
 * Represents java constructor declaration scope.
 * @author alex.bereznevatiy@gmail.com
 */
public class JavaConstructor extends JavaMethod {
	// TODO: verify if we realy need this variable
	private List<String> parameters;
	private List<String> rawParams;
	private boolean varArgs;

	JavaConstructor(String className, JavaTypeScope parent, int start, List<JavaVariable> vars, boolean varArgs) {
		super(Name.getSimpleName(className), parent, start, fixParameters(parent, vars), className, varArgs);
		parameters = new LinkedList<String>();
		rawParams = new LinkedList<String>();
		if (parent.isEnum()) {
			parameters.add("java.lang.String");
			parameters.add("int");
		}
		this.varArgs = varArgs;
	}

	private static List<JavaVariable> fixParameters(JavaTypeScope parent,
			List<JavaVariable> vars) {
		if (parent.isEnum()) {
			vars.add(0, new JavaVariable("name", "java.lang.String", -1, null));
			vars.add(1, new JavaVariable("ordinal", "int", -1, null));
		}
		return vars;
	}

	@Override
	void endScope(int end) {
		super.endScope(end);
		if (!varArgs && !parameters.isEmpty()) {
			varArgs = vars.get(parameters.size() - 1).isVarArg();
		}
		parameters = Collections.unmodifiableList(parameters);
	}
	/*
	 * TODO: get rid of this method and related functionality since this is duplicated code from JavaMethod class
	 */
	void addParameter(String param) {
		parameters.add(Name.getRawName(resolveType(param)));
	}
	
	void addRawParameter(String param) {
		rawParams.add(param);
	}

	@Override
	protected String[] getRawParams() {
		return rawParams.toArray(new String[rawParams.size()]);
	}

	@Override
	public boolean isVarArgs() {
		return varArgs;
	}

	@Override
	public String[] getParametersTypes() {
		return parameters.toArray(
			new String[parameters.size()]);
	}
	/**
	 * Constructor always "returns" instances of the type it creates
	 */
	public JavaTypeScope getGenericReturnType() {
		return getParent();
	}
}
